// pages/recharge/recharge.js
const app = getApp();
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    item: ["10", "50", "100", "200", "500", "1000", "2000", "5000"],
    indexd:0,
    getconzhi: app.data.url +'sheyingjia/Xmychongzhi/getconzhi',
    chongzhi: app.data.url + 'sheyingjia/Xmychongzhi/chongzhi',
    shuju:[],
    congzhijine:0,
  },
  // 选择充值金额
  datass:function(e){
    var index = e.currentTarget.dataset.index;
    var shuju = this.data.shuju;

    console.log(index);
  this.setData({
    indexd: index,
    congzhijine: shuju[index].jiage
  })
  },
  shurujine:function(e){
    console.log(e);
    var congzhijine = e.detail.value;
    // congzhijine = parseFloat(congzhijine);
    this.setData({ congzhijine: congzhijine});
  },
 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var user = wx.getStorageSync('user');
    ea.qinqiu(that.data.getconzhi,{token:user.token},function(data){
      console.log(data[0].jiage);
      that.setData({ shuju: data, congzhijine: data[0].jiage});
    })
  },
  //点击充值
  chongzhi:function(){
    var congzhijine = this.data.congzhijine;
    var that = this;
    congzhijine = parseFloat(congzhijine);
    that.setData({ congzhijine: congzhijine});
    console.log(that.data.chongzhi);
    // return
    var user = wx.getStorageSync('user');
    if (congzhijine<=0){
      wx.showToast({
        title: '请输入正确的金额',
        icon:'none'
      })
      return;
    }
    ea.qinqiu(that.data.chongzhi, { token: user.token, jine: congzhijine},function(data){
      console.log(data);
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.paySign,
        success:function(res){
          console.log(res);
          wx.showToast({
            title: '充值成功',
          })
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})